package net.lastcoder.ifserver.util;


import net.lastcoder.ifserver.Globals;
import net.lastcoder.ifserver.Server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Properties;

/**
 * auth: WenYF
 * date: 2017/9/20
 */

public class SystemCmd {
    private static final Logger logger = LoggerFactory.getLogger(Server.class);

    /**没有错误返回true,否则是false
     * @return  0, boolean, true or false.   1, string, ret
     */
    public static Object[] cmds(String[] cmds) {
        OutputStream out = null;
        InputStream is = null;
        InputStream errIs = null;
        Object[] rets = new Object[2];
        String strRet = "";
        String errRet = "";

        if (cmds == null || cmds.length == 0) {
            rets[0] = false;
            rets[1] = "cmds is nil";
            return rets;
        }

        try {
            Process process = null;
            File file = new File((Globals.getHomeDirectory() + File.separator + "res" + File.separator + "fzxy"));
            Properties props = System.getProperties();
            if (props.getProperty("os.name").toLowerCase().contains("win")) {
                process = Runtime.getRuntime().exec("cmd.exe");
                out = process.getOutputStream();
                PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)), true);

                for (String cmd : cmds) {
                    pw.println(cmd);
                }

                pw.println("exit");
            } else {
                process = Runtime.getRuntime().exec("/bin/bash", null, file);
                out = process.getOutputStream();
                PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)), true);

                for (String cmd : cmds) {
                    pw.println(cmd);
                }

                pw.println("exit");
            }

            is = process.getInputStream();
            errIs = process.getErrorStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = in.readLine()) != null) {
                strRet = strRet + line;
            }

            BufferedReader errIn = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = errIn.readLine()) != null) {
                errRet = errRet + line;
            }

            process.waitFor();

            rets[0] = true;
            rets[1] = "ret:" + strRet + ",err:" + errRet;

        } catch (Exception e) {
            rets[0] = false;
            rets[1] = e.getMessage();
            logger.error(e.getMessage(),    e);
        } finally {
            IOUtil.closeSilently(is);
            IOUtil.closeSilently(errIs);
            IOUtil.closeSilently(out);
        }

        return rets;
    }
}
